//View for seeing top artists

import java.util.ArrayList;

import processing.core.PImage;
import controlP5.*;

public class TopView extends View {
	ControlP5 controlP5;
	
	private ListBox LB_Top;	
	private controlP5.Button B_Remove, B_Wave, B_Bar, B_Back1, B_Back2;
	private GraphSet mainGraph; 		//Wave Graph & Bar Graph
	private ArtistView artistInfo;
	
	private boolean showGraphs = true;	//View wave or bar graphs
	private PImage top40, artistTitle;
	private ArrayList<ArtistData> artistHistory;
	private ArrayList<ArtistData> viewArtistList;
	
	private boolean empty = false;
	private short count = 0;
	
	public TopView(int _x1, int _y1, int _x2, int _y2, Filters f) {
		x1 = _x1;
		y1 = _y1;
		x2 = _x2;
		y2 = _y2;
		FLT = f;
		
		controlP5 = new ControlP5(GLOBAL.processing);
			controlP5.setAutoDraw(false);
			
		width = x2 - x1;
		height = y2 - y1;
		
		setup();
	}
	
	private void populateList() {
		viewArtistList = new ArrayList<ArtistData>();
		for(int x=0; x<radioGaGa.ArtistList.size(); ++x) {
			if(radioGaGa.ArtistList.get(x).getCount() < 2)
				break;
			viewArtistList.add(radioGaGa.ArtistList.get(x));
			LB_Top.addItem(radioGaGa.ArtistList.get(x).getName(), x);
			//LB_Top.addItem(radioGaGa.ArtistList.get(x).getBandName(), x); //reflects new ArtistData method IGG
		}
	}
	
	private void populateScrollingList(){
		
		try {
			LB_Top.clear();
		} catch (Exception e) {
			if(count < 10) {
				populateScrollingList();
				count++;
			}
			return;
		}
		
		for(int x=0; x< viewArtistList.size(); ++x) {
			LB_Top.addItem(viewArtistList.get(x).getName(), x);
		}
		count = 0;
	}
	
	public void resize(int _x1, int _y1, int _x2, int _y2) {
		x1 = _x1;
		y1 = _y1;
		x2 = _x2;
		y2 = _y2;
		
		width = x2 - x1;
		height = y2 - y1;
		
		LB_Top.setHeight(height-40);
		
		if(artistInfo != null)
			artistInfo.resize(x1+250, y1+80, x1+700, y2-50);
		
		moveButtons();
		
		if(!empty)
			mainGraph.move(x1+250, y1+200, x1+700, y1+225);
	}
	
	private void setup() {	
		LB_Top = controlP5.addListBox("vw_top", x1+20, y1+30, 150, height-40);
			LB_Top.setLabel("Top Artists");
			LB_Top.setItemHeight(15);
			LB_Top.setColorForeground(GLOBAL.colorTabSelected);
        	LB_Top.setColorActive(GLOBAL.colorTabMouseOver);
        	LB_Top.setColorBackground(GLOBAL.colorListBox);
        	LB_Top.setColorLabel(0);
        	LB_Top.setColorValue(0);
        	
		B_Remove = controlP5.addButton("remove", 0, x2-35, y1+30, 25, 25);
			B_Remove.setImages(GLOBAL.processing.loadImage("minus2.gif"), GLOBAL.processing.loadImage("minus.gif"), GLOBAL.processing.loadImage("minus3.gif"));
	
		B_Wave = controlP5.addButton("vw_wave", 0, x1+420, y1+75, 50, 25);
			B_Wave.setLabel("Wave");
			GLOBAL.setButtonColors(B_Wave);
			B_Wave.setColorBackground(GLOBAL.colorTabSelected);
		
		B_Bar = controlP5.addButton("vw_bar", 0, x1+480, y1+75, 50, 25);
			B_Bar.setLabel("In Order");
			GLOBAL.setButtonColors(B_Bar);
			
		B_Back1 = controlP5.addButton("vw_back1", 0, x2-95, y1+30, 25, 25);
			B_Back1.setImages(GLOBAL.processing.loadImage("back1_1.gif"), GLOBAL.processing.loadImage("back1_2.gif"), GLOBAL.processing.loadImage("back1_3.gif"));
			GLOBAL.setButtonColors(B_Back1);
			B_Back1.setVisible(false);
			
		B_Back2 = controlP5.addButton("vw_back2", 0, x2-65, y1+30, 25, 25);
			B_Back2.setImages(GLOBAL.processing.loadImage("back2_1.gif"), GLOBAL.processing.loadImage("back2_2.gif"), GLOBAL.processing.loadImage("back2_3.gif"));
			GLOBAL.setButtonColors(B_Back2);
			B_Back2.setVisible(false);
			
		top40 = GLOBAL.processing.loadImage("top40.gif");
		artistTitle = GLOBAL.processing.loadImage("artistInfo.gif");
		
		artistHistory = new ArrayList<ArtistData>();
		
		populateList();
		
		if(viewArtistList.size() == 0) {
			System.out.println("empty.");
			empty = true;
			return;
		}

		initGraphs();
	}
	
	private void initGraphs() {
		mainGraph = new GraphSet(x1+250, y1+200, x1+700, y1+225);
		
		for(int x=0; x<40; ++x) {
			if(x >= viewArtistList.size()) { break; }
			mainGraph.addVal(viewArtistList.get(x).getCount());
			mainGraph.addArtist(viewArtistList.get(x));
		}
		mainGraph.createGraphs();
	}
	
	private void moveButtons() {
		LB_Top.setPosition(x1+20, y1+30);
		B_Remove.setPosition(x2-35, y1+30);
		B_Wave.setPosition(x1+420, y1+75);
		B_Bar.setPosition(x1+480, y1+75);
		B_Back1.setPosition(x2-95, y1+30);
		B_Back2.setPosition(x2-65, y1+30);
	}
	
	private void drawTitle() {
		GLOBAL.processing.fill(0);
		GLOBAL.processing.text(FLT.generateTitle(), x1+20,y1+15);//x1+200, y1+30);
	}
	
	public void draw() {
		if(empty) {
			GLOBAL.processing.fill(0);
			GLOBAL.processing.text("No " + FLT.generateTitle(), x1+25, y1+25);
			return;
		}
		
		controlP5.draw();
		drawTitle();
		
		if(showGraphs) {
			//Graph Title
			GLOBAL.processing.image(top40, x1+440, y1+50);
			mainGraph.draw();
		}
		else {	//Viewing Artist
			GLOBAL.processing.image(artistTitle, x1+440, y1+30);
			if(artistInfo != null)
				artistInfo.draw();
			B_Back2.setVisible(true);
			
			GLOBAL.processing.textFont(GLOBAL.fontSmall);
			GLOBAL.processing.text("top40", x2-66, y1+28);
			
			if(B_Back1.isVisible()) {
				GLOBAL.processing.text("back", x2-95, y1+28);
			}
			GLOBAL.processing.textFont(GLOBAL.fontSegoe);
		}
		
	
	}
	
	//Button Events
	public void handleEvent(String event) {
		if(event.equals("vw_bar")) {
			B_Bar.setColorBackground(GLOBAL.colorTabSelected);
			B_Wave.setColorBackground(GLOBAL.colorTabDeselected);	
			mainGraph.setMode(false);
			return;
		}
		if(event.equals("vw_wave")) {
			B_Bar.setColorBackground(GLOBAL.colorTabDeselected);
			B_Wave.setColorBackground(GLOBAL.colorTabSelected);	
			mainGraph.setMode(true);
			return;
		}
		//Back to Top 40
		if(event.equals("vw_back2")) {
			artistHistory.clear();
			B_Back1.setVisible(false);
			B_Back2.setVisible(false);
			B_Bar.setVisible(true);
			B_Wave.setVisible(true);
			showGraphs = true;
			if(mainGraph.getMode() == true)
				this.handleEvent("vw_wave");
			else
				this.handleEvent("vw_bar");
		}
		//Back to Previous Artist
		if(event.equals("vw_back1")) {
			artistHistory.remove(artistHistory.size()-1);	//Remove currently displayed artist from history
			artistInfo = new ArtistView(x1+250, y1+80, x1+700, y2-50, artistHistory.get(artistHistory.size()-1));	//display previous artistt
			
			if(artistHistory.size() == 1)
				B_Back1.setVisible(false);
		}
		
		//Ignore Artist
		if(event.equals("vw_ignore")) {
			if(!GLOBAL.IGNORE.contains(artistInfo.getArtist()))
				GLOBAL.IGNORE.add(artistInfo.getArtist());		//Add Artist to (Global) 'Ignore' list
			
			viewArtistList.remove(artistInfo.getArtist());	//Remove artist from *this view
			populateScrollingList();
			initGraphs();	//Re-create (top40) graphs

			if(artistHistory.size() > 1)
				this.handleEvent("vw_back1");	//Go to previous artist
			else
				this.handleEvent("vw_back2");	//Go to top forty
			return;
		}
		
	}
	
	//Artist Selection Events
	public void handleEvent(int event) {
		//Hide Graphs & Graph Buttons
		showGraphs = false;
		B_Wave.setVisible(false);
		B_Bar.setVisible(false);
		B_Back2.setVisible(true);
		
		//distplay artist # event
		artistInfo = new ArtistView(x1+250, y1+80, x1+700, y2-50, viewArtistList.get(event));
		
		//add Artist to history (unless user chose same Artist 2x in a row)
		if(artistHistory.isEmpty() || !artistHistory.get(artistHistory.size()-1).equals(viewArtistList.get(event)))
			artistHistory.add(viewArtistList.get(event));
		
		if(artistHistory.size() == 2) {
			B_Back1.setVisible(true);
		}
	}
}
